# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

require 'AuthCheck'

class ApplicationController < ActionController::Base
  # Pick a unique cookie name to distinguish our session data from others'
  session :session_key => '_ABDSuite_session_id'
  
  protected

  def authenticate_check(exp,redirect)
    unless session[:user]
      flash[:notice] = "Desculpe, você precisar entrar com o usuário e senha para acessar essa área.";
      redirect_to redirect
    else
    logger.debug(AuthCheck.run?(exp, session[:user].capabilite_strs))      
      unless AuthCheck.run?(exp, session[:user].capabilite_strs)
        flash[:notice] = "Desculpe, você não tem permissão para acessar essa área, contacte o administrador.";
        redirect_to redirect
      end
    end
  end

  def authenticate_check?(exp)
    unless session[:user]
      logger.debug("[DEBUG] authenticate_check?(#{exp}): session invalid, return false")
      return false
    else
      unless AuthCheck.run?(exp, session[:user].capabilite_strs)
        logger.debug("[DEBUG] authenticate_check?(#{exp},#{session[:user].capabilite_strs}): AuthCheck invalid, return false")
        return false
      end
    end
    logger.debug("[DEBUG] authenticate_check?(#{exp}): return true")
    return true
  end

  def get_parameter(module_name, parameter)
    parameter = Parameter.get(module_name, parameter)
    return parameter[:value]
  end  
end
